﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using OnlineRailway.EF;
using OnlineRailway.Administrator;

namespace OnlineRailway.UI.Administrator
{
    public partial class ScheduleCreate : System.Web.UI.Page
    {
        private SqlCommand cmd;
        private DataSet ds= new DataSet();
        private SqlDataAdapter dataAdapter;
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMes.Text = "";
            if (!IsPostBack)
            {
                BindStation();
            }
        }
        void BindStation()
        {
            try
            {
                if (Connect.Conn.State==ConnectionState.Closed)
                {
                    Connect.Conn.Open();
                }
                cmd = new SqlCommand("SELECT [StationName] FROM Stations",Connect.Conn);
                dataAdapter = new SqlDataAdapter(cmd);
                dataAdapter.Fill(ds);
                ddlStartStation.DataSource = ds;
                ddlStartStation.DataTextField = "StationName";
                ddlStartStation.DataValueField = "StationName";
                ddlStartStation.DataBind();
                ddlEndStation.DataSource = ds;
                ddlEndStation.DataTextField = "StationName";
                ddlEndStation.DataValueField = "StationName";
                ddlEndStation.DataBind();
            }
            catch (Exception ex)
            {
                lblMes.Text = "Error: " + ex.Message;
            }
            finally
            {
                Connect.Conn.Close();
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                String sn = Request.Form["txtScheduleName"];
                using (var context = new RailwayOnlineEntities())
                {
                    var obj = new TrainSchedule
                    {
                        ScheduleName = Request.Form["txtScheduleName"],
                        StartStation = ddlStartStation.Text,
                        EndStation = ddlEndStation.Text,
                        BetweenStation = Request.Form["txtBetweenStation"],
                        Distances = Request.Form["txtDistances"],
                        LongTime = Convert.ToInt32(Request.Form["txtLongTime"]),
                        Fare = Convert.ToInt32(Request.Form["txtFare"]),
                        Description = Request.Form["txtaDes"],
                        CreateDate = DateTime.Now,
                        Status = slStatus.Value
                    };
                    context.TrainSchedules.AddObject(obj);
                    context.SaveChanges();
                    lblMes.Text = "Schedule " + sn + " has been created!";
                    lblMes.Attributes.Add("Style","Color: Green;");
                }
            }
            catch (Exception ex)
            {
                lblMes.Text = "Error: " + ex.Message;
                lblMes.Attributes.Add("Style","Color: Red;");
            }
            
        }

    }
}